﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
using DiscoveryLogic.DataServices.DataSources;


namespace DiscoveryLogic.UI.Layouts.DataAccesses
{
    public class BillPaymentManager
    {
        List<BillPayee> listOfLocalBillPayees = null;

        #region Constructors

        public BillPaymentManager()
        {
        }

        #endregion

        #region Private Helper Methods

        private List<BillPayeeForDataBinding> ConvertToPayeeForDataBinding(List<BillPayee> listBP)
        {
            List<BillPayeeForDataBinding> listOfPayees = new List<BillPayeeForDataBinding>();

            foreach (BillPayee bp in listBP)
            {
                listOfPayees.Add(new BillPayeeForDataBinding(bp.Name, bp.AccountNumber, bp.IsRecurring, bp.History));
            }

            return listOfPayees;
        }
        #endregion

        #region Public Methods

        // Calls webservice and returns a list of BillPayee objects
        public List<BillPayeeForDataBinding> GetPayees()
        {
            //if ( (!SL2Samples.DesignTimeHelper.IsInDesignMode) &&   ((App)Application.Current).UseWebServices == true)
            //{
            //    return ConvertToPayeeForDataBinding(WSPayees);
            //}
            //else
            //{
            //    return ConvertToPayeeForDataBinding(LocalPayees);
            //}
            return ConvertToPayeeForDataBinding(LocalPayees);
        }

        public List<BillPayee> WSPayees
        {
            get
            {

                BillPayService billService = new BillPayService();
                List<BillPayee> list = new List<BillPayee>();
                list.AddRange(billService.GetPayees());

                AdjustDates(list);

                return list;
            }
        }

        public List<BillPayee> LocalPayees
        {
            get
            {
                if (listOfLocalBillPayees == null)
                {
                    listOfLocalBillPayees = new List<BillPayee>(LocalDataFiles.LoadData<BillPayee[]>("DiscoveryLogic.UI.Layouts.LocalDataFiles.BillPayee.xml"));

                    AdjustDates(listOfLocalBillPayees);
                   }


                return listOfLocalBillPayees;

            }
        }

        private void AdjustDates(List<BillPayee> list)
        {
            //Adjust dates
            List<DateTime> originalDates = new List<DateTime>();
            list.ForEach(l => l.History.ForEach(h => originalDates.Add(h.Date)));

            list.ForEach(
                l => l.History.ForEach(
                    h => h.Date = Utils.MoveDateWithinRange(originalDates, h.Date, DateTime.Today, DateTime.Today.AddDays(90))));
        }

        #endregion
    }
}
